﻿using Sorting.App.Models;

using Sorting.App.Services;
using Flurl.Http;
using Modules.Units;
using Sorting.App.Models.Model;
using Sorting.App.Models.Entity;

namespace Sorting.App.Services
{


    /// <summary>
    /// 登录服务
    /// </summary>
    public class WardAreaService
    {

        private ServiceApis serviceApis = new ServiceApis();


        /// <summary>
        /// 获取病区信息
        /// </summary>
        /// <returns></returns>
        public async Task<IEnumerable<WardAreaEntity>> GetWardAreaEntities()
        {

            var res = await serviceApis.GetWardAreaEntitiesApi
                 .WithOAuthBearerToken(AppsettingUnits.Token)
                .PostAsync()
                .ReceiveJson<ResfulModel<IEnumerable<WardAreaEntity>>>();

            return res.data;
        }

        /// <summary>
        /// 编辑病区
        /// </summary>
        /// <param name="wardAreaItem"></param>
        /// <returns></returns>
        public async Task<ResfulModel<bool>> AddWardArea(WardAreaEntity wardAreaItem)
        {
           return await serviceApis.AddWardAreaApi
                 .WithOAuthBearerToken( AppsettingUnits.Token)
                .PostJsonAsync(wardAreaItem)
               .ReceiveJson<ResfulModel<bool>>();
        }


        /// <summary>
        /// 修改病区
        /// </summary>
        /// <param name="wardAreaItem"></param>
        /// <returns></returns>
        public async Task<ResfulModel<bool>>  EditWardArea(WardAreaEntity wardAreaItem)
        {
            return await serviceApis.EditWardAreaApi
                 .WithOAuthBearerToken( AppsettingUnits.Token)
               .PostJsonAsync(wardAreaItem)
              .ReceiveJson<ResfulModel<bool>>();
        }


        /// <summary>
        /// 删除病区
        /// </summary>
        /// <param name="wardAreaItem"></param>
        /// <returns></returns>
        public async Task<ResfulModel<bool>> DelWardArea(WardAreaEntity wardAreaItem)
        {
            return await serviceApis.DelWardAreaApi
                 .WithOAuthBearerToken( AppsettingUnits.Token)
               .PostJsonAsync(wardAreaItem)
              .ReceiveJson<ResfulModel<bool>>();
        }
        /// <summary>
        /// 根据条码获取病区信息
        /// </summary>
        /// <param name="wardAreaItem"></param>
        /// <returns></returns>
        public BarcodeWardDto GetTQWareNoBySnForAsynApi(string SnNo)
        {
            var res = serviceApis.GetTQWareNoBySnForAsynApi
                  .WithOAuthBearerToken(AppsettingUnits.Token)
                 .SetQueryParams(new
                 {
                     SnNo = SnNo,
                 }).PostAsync()
                 .ReceiveJson<ResfulModel<BarcodeWardDto>>();

            return res.Result.data;
        }
        //public async Task<BarcodeWardDto> GetTQWareNoBySnForAsynApi(string SnNo)
        //{
        //    var res = await serviceApis.GetTQWareNoBySnForAsynApi
        //          .WithOAuthBearerToken(AppsettingUnits.Token)
        //         .SetQueryParams(new
        //         {
        //             SnNo = SnNo,
        //         }).PostAsync()
        //         .ReceiveJson<ResfulModel<BarcodeWardDto>>();

        //    return res.data;
        //}

        /// <summary>
        /// 获取所有病区信息
        /// </summary>
        /// <param name="wardAreaItem"></param>
        /// <returns></returns>
        public async Task<IEnumerable<WardArea>> GetWardAreas(string BatchNo)
        {
            var res = await serviceApis.GetWardAreasApi
                 .WithOAuthBearerToken(AppsettingUnits.Token)
                 .SetQueryParams(new
                 {
                     BatchNo = BatchNo,
                 }).PostAsync()
                 .ReceiveJson<ResfulModel<IEnumerable<WardArea>>>();

            return res.data;
        }
        //public async Task<IEnumerable<WardArea>> GetWardAreas()
        //{
        //    var res = await serviceApis.GetWardAreasApi
        //         .WithOAuthBearerToken(AppsettingUnits.Token)
        //         .PostAsync()
        //         .ReceiveJson<ResfulModel<IEnumerable<WardArea>>>();

        //    return res.data;
        //}
        /// <summary>
        /// 更新分拣状态
        /// </summary>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<ResfulModel<bool>> UpdateWardAreaState(string barcode,string fjr)
        {
            return await serviceApis.UpdateWardAreaState
                 .WithOAuthBearerToken(AppsettingUnits.Token)
            .SetQueryParams(new
            {
                barcode = barcode,
                fjr= fjr
            }).PostAsync()
            .ReceiveJson<ResfulModel<bool>>();
        }
    }
}
